import WS, { type Params } from './core';
// 创建WebSocket实例
export function createWebSocket(params: Params) {
  const arr =
    WS.ONLINE &&
    WS.ONLINE.filter((socket) => {
      return socket.url === params.url;
    });
  const existSocket = arr && arr[0];
  let instance: WS | null = null;
  // 实例存在
  if (existSocket) {
    instance = existSocket;
  } else {
    // 实例存在不存在
    instance = new WS(params);
    const closeFn = instance.close;
    instance.close = function () {
      closeFn.call(instance);
      instance = null;
    };
  }
  return instance;
}
